package com.qf.service.impl;

import com.qf.dao.UserMapper;
import com.qf.pojo.User;
import com.qf.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.r2dbc.core.R2dbcEntityTemplate;
import org.springframework.data.relational.core.query.Criteria;
import org.springframework.data.relational.core.query.Query;
import org.springframework.data.relational.core.query.Update;
import org.springframework.stereotype.Service;
import org.yaml.snakeyaml.events.Event;
import reactor.core.publisher.Flux;
import reactor.core.publisher.Mono;

/*
 *@Description:
 *@author:刘哥
 *@Date:86180 2022/9/22 19:56
 */
@Service
public class UserServiceImpl implements UserService {
//    private UserMapper userMapper;

//    @Autowired
//    public void setUserMapper(UserMapper userMapper) {
//        this.userMapper = userMapper;
//    }

    private R2dbcEntityTemplate r2dbcEntityTemplate;
    @Autowired
    public void setR2dbcEntityTemplate(R2dbcEntityTemplate r2dbcEntityTemplate) {
        this.r2dbcEntityTemplate = r2dbcEntityTemplate;
    }

    @Override
    public Mono<User> findById(Integer id) {
        //默认情况下类名首字母小写就是表名
//        return r2dbcEntityTemplate.selectOne(Query.query(Criteria.where("userId").is(id)),User.class);

        return r2dbcEntityTemplate.select(User.class)
                .from("user")
                .matching(Query.query(Criteria.where("userId").is(id)))
                .one();
    }
    @Override
    public void addUser(User user) {
        //默认就是对象的类型就是表名,然后对象中的属性名就是列名
        // r2dbcEntityTemplate.insert(user);
        r2dbcEntityTemplate.insert(User.class).into("user").using(user).subscribe();
    }

    @Override
    public Flux<User> findAllUser() {
        return r2dbcEntityTemplate.select(User.class).from("user").all();
    }

    @Override
    public Flux<User> findByNameLike(String name) {
        return r2dbcEntityTemplate.select(User.class).from("user").matching(Query.query(Criteria.where("name").like("%"+name+"%"))).all();
    }

    @Override
    public Mono<User> findByNameEquals(String name) {
        return r2dbcEntityTemplate.select(User.class).from("user").matching(Query.query(Criteria.where("username").is(name))).one();

    }

    @Override
    public Mono<Integer> deleteByNameEquals(String name) {
        return r2dbcEntityTemplate
                .delete(User.class)
                .from("user")
                .matching(Query.query(Criteria.where("name").is(name)))
                .all();
    }

    @Override
    public void updateUser(User user) {
        r2dbcEntityTemplate.update(User.class)
                .inTable("user")
                .matching(Query.query(Criteria.where("userId").is(user.getUserId())))
                .apply(Update.update("username", user.getUsername())).subscribe();

    }

    @Override
    public Mono<User> findFirstByNameLike(String name) {
        return r2dbcEntityTemplate.select(User.class).from("user").matching(Query.query(Criteria.where("username").like("%"+name+"%"))).first();

    }
}
